In [8]:
import pandas as pd, numpy as np
In [9]:
import matplotlib.pyplot as plt, seaborn as sns
In [10]:
%matplotlib inline
In [3]:
# The Iris Setosa
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg'
Image(url,width=300,height=300)
Out[3]:
In [5]:
# The Iris Versicolor
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/4/41/Iris_versicolor_3.jpg'
Image(url,width=300,height=300)
Out[5]:
In [6]:
# The Iris Virginica
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/9/9f/Iris_virginica.jpg'
Image(url,width=300,height=300)
Out[6]:
In [11]:
iris = sns.load_dataset('iris')
In [12]:
iris.head()
Out[12]:
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
In [96]:
sns.pairplot(iris,hue='species',palette='Dark2')
C:\Users\User\Anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval
Out[96]:
<seaborn.axisgrid.PairGrid at 0xb189870>
In [100]:
setosa = iris[iris.species == "setosa"]
sns.kdeplot(setosa.sepal_width,setosa.sepal_length,cmap='plasma',shade=True,shade_lowest=False)
Out[100]:
<matplotlib.axes._subplots.AxesSubplot at 0xcd12b50>
In [28]:
flower_feats = ['species']
In [30]:
final_data = pd.get_dummies(iris,columns=flower_feats,drop_first=True)
In [33]:
from sklearn.model_selection import train_test_split
In [80]:
#iris_feats = ['species']
#iris_species = pd.DataFrame(iris['species'])
#iris_species = pd.get_dummies(iris,columns=iris_feats,drop_first=True)
In [108]:
X = iris.drop('species',axis=1)
y = iris['species']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
In [109]:
from sklearn.svm import SVC
In [110]:
model = SVC()
In [111]:
model.fit(X_train,y_train)
Out[111]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
In [112]:
predictions = model.predict(X_test)
In [113]:
from sklearn.metrics import classification_report,confusion_matrix
In [114]:
print(confusion_matrix(y_test,predictions))
print('\n')
print(classification_report(y_test,predictions))
[[13  0  0]
 [ 0 20  0]
 [ 0  0 12]]


             precision    recall  f1-score   support

     setosa       1.00      1.00      1.00        13
 versicolor       1.00      1.00      1.00        20
  virginica       1.00      1.00      1.00        12

avg / total       1.00      1.00      1.00        45

In [88]:
from sklearn.model_selection import GridSearchCV
In [115]:
param_grid = {'C':[0.1,1.0,10,100],'gamma':[1.0,0.1,0.01,0.001]}
In [117]:
grid = GridSearchCV(SVC(),param_grid,verbose=2)
In [118]:
grid.fit(X_train,y_train)
Fitting 3 folds for each of 16 candidates, totalling 48 fits
[CV] C=0.1, gamma=1.0 ................................................
[CV] ................................. C=0.1, gamma=1.0, total=   0.0s
[CV] C=0.1, gamma=1.0 ................................................
[CV] ................................. C=0.1, gamma=1.0, total=   0.0s
[CV] C=0.1, gamma=1.0 ................................................
[CV] ................................. C=0.1, gamma=1.0, total=   0.0s
[CV] C=0.1, gamma=0.1 ................................................
[CV] ................................. C=0.1, gamma=0.1, total=   0.0s
[CV] C=0.1, gamma=0.1 ................................................
[CV] ................................. C=0.1, gamma=0.1, total=   0.0s
[CV] C=0.1, gamma=0.1 ................................................
[CV] ................................. C=0.1, gamma=0.1, total=   0.0s
[CV] C=0.1, gamma=0.01 ...............................................
[CV] ................................ C=0.1, gamma=0.01, total=   0.0s
[CV] C=0.1, gamma=0.01 ...............................................
[CV] ................................ C=0.1, gamma=0.01, total=   0.0s
[CV] C=0.1, gamma=0.01 ...............................................
[CV] ................................ C=0.1, gamma=0.01, total=   0.0s
[CV] C=0.1, gamma=0.001 ..............................................
[CV] ............................... C=0.1, gamma=0.001, total=   0.0s
[CV] C=0.1, gamma=0.001 ..............................................
[CV] ............................... C=0.1, gamma=0.001, total=   0.0s
[CV] C=0.1, gamma=0.001 ..............................................
[CV] ............................... C=0.1, gamma=0.001, total=   0.0s
[CV] C=1.0, gamma=1.0 ................................................
[CV] ................................. C=1.0, gamma=1.0, total=   0.0s
[CV] C=1.0, gamma=1.0 ................................................
[CV] ................................. C=1.0, gamma=1.0, total=   0.0s
[CV] C=1.0, gamma=1.0 ................................................
[CV] ................................. C=1.0, gamma=1.0, total=   0.0s
[CV] C=1.0, gamma=0.1 ................................................
[CV] ................................. C=1.0, gamma=0.1, total=   0.0s
[CV] C=1.0, gamma=0.1 ................................................
[CV] ................................. C=1.0, gamma=0.1, total=   0.0s
[CV] C=1.0, gamma=0.1 ................................................
[CV] ................................. C=1.0, gamma=0.1, total=   0.0s
[CV] C=1.0, gamma=0.01 ...............................................
[CV] ................................ C=1.0, gamma=0.01, total=   0.0s
[CV] C=1.0, gamma=0.01 ...............................................
[CV] ................................ C=1.0, gamma=0.01, total=   0.0s
[CV] C=1.0, gamma=0.01 ...............................................
[CV] ................................ C=1.0, gamma=0.01, total=   0.0s
[CV] C=1.0, gamma=0.001 ..............................................
[CV] ............................... C=1.0, gamma=0.001, total=   0.0s
[CV] C=1.0, gamma=0.001 ..............................................
[CV] ............................... C=1.0, gamma=0.001, total=   0.0s
[CV] C=1.0, gamma=0.001 ..............................................
[CV] ............................... C=1.0, gamma=0.001, total=   0.0s
[CV] C=10, gamma=1.0 .................................................
[CV] .................................. C=10, gamma=1.0, total=   0.0s
[CV] C=10, gamma=1.0 .................................................
[CV] .................................. C=10, gamma=1.0, total=   0.0s
[CV] C=10, gamma=1.0 .................................................
[CV] .................................. C=10, gamma=1.0, total=   0.0s
[CV] C=10, gamma=0.1 .................................................
[CV] .................................. C=10, gamma=0.1, total=   0.0s
[CV] C=10, gamma=0.1 .................................................
[CV] .................................. C=10, gamma=0.1, total=   0.0s
[CV] C=10, gamma=0.1 .................................................
[CV] .................................. C=10, gamma=0.1, total=   0.0s
[CV] C=10, gamma=0.01 ................................................
[CV] ................................. C=10, gamma=0.01, total=   0.0s
[CV] C=10, gamma=0.01 ................................................
[CV] ................................. C=10, gamma=0.01, total=   0.0s
[CV] C=10, gamma=0.01 ................................................
[CV] ................................. C=10, gamma=0.01, total=   0.0s
[CV] C=10, gamma=0.001 ...............................................
[CV] ................................ C=10, gamma=0.001, total=   0.0s
[CV] C=10, gamma=0.001 ...............................................
[CV] ................................ C=10, gamma=0.001, total=   0.0s
[CV] C=10, gamma=0.001 ...............................................
[CV] ................................ C=10, gamma=0.001, total=   0.0s
[CV] C=100, gamma=1.0 ................................................
[CV] ................................. C=100, gamma=1.0, total=   0.0s
[CV] C=100, gamma=1.0 ................................................
[CV] ................................. C=100, gamma=1.0, total=   0.0s
[CV] C=100, gamma=1.0 ................................................
[CV] ................................. C=100, gamma=1.0, total=   0.0s
[CV] C=100, gamma=0.1 ................................................
[CV] ................................. C=100, gamma=0.1, total=   0.0s
[CV] C=100, gamma=0.1 ................................................
[CV] ................................. C=100, gamma=0.1, total=   0.0s
[CV] C=100, gamma=0.1 ................................................
[CV] ................................. C=100, gamma=0.1, total=   0.0s
[CV] C=100, gamma=0.01 ...............................................
[CV] ................................ C=100, gamma=0.01, total=   0.0s
[CV] C=100, gamma=0.01 ...............................................
[CV] ................................ C=100, gamma=0.01, total=   0.0s
[CV] C=100, gamma=0.01 ...............................................
[CV] ................................ C=100, gamma=0.01, total=   0.0s
[CV] C=100, gamma=0.001 ..............................................
[CV] ............................... C=100, gamma=0.001, total=   0.0s
[CV] C=100, gamma=0.001 ..............................................
[CV] ............................... C=100, gamma=0.001, total=   0.0s
[CV] C=100, gamma=0.001 ..............................................
[CV] ............................... C=100, gamma=0.001, total=   0.0s
[Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:    0.0s remaining:    0.0s
[Parallel(n_jobs=1)]: Done  48 out of  48 | elapsed:    0.0s finished
Out[118]:
GridSearchCV(cv=None, error_score='raise',
       estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False),
       fit_params=None, iid=True, n_jobs=1,
       param_grid={'C': [0.1, 1.0, 10, 100], 'gamma': [1.0, 0.1, 0.01, 0.001]},
       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',
       scoring=None, verbose=2)
In [119]:
grid.best_params_
Out[119]:
{'C': 1.0, 'gamma': 0.1}
In [120]:
grid.best_estimator_
Out[120]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma=0.1, kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
In [121]:
grid_predictions = grid.predict(X_test)
In [122]:
print(confusion_matrix(y_test,grid_predictions))
print('\n')
print(classification_report(y_test,grid_predictions))
[[13  0  0]
 [ 0 19  1]
 [ 0  0 12]]


             precision    recall  f1-score   support

     setosa       1.00      1.00      1.00        13
 versicolor       1.00      0.95      0.97        20
  virginica       0.92      1.00      0.96        12

avg / total       0.98      0.98      0.98        45